import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;

public class Test4 {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        List<ListSql> list =new ArrayList<>();
        String url = "jdbc:mysql://localhost:3306/jdbcdemo";
        try (Connection conn = DriverManager.getConnection(url,"root","");
             Statement stat = conn.createStatement()){
            ResultSet set = stat.executeQuery("select * from user");
//            while (set.next()) { // 类比 hesNext() next() 整合到一起了
//               int deptno = set.getInt("DEPTNO");
//               String dname = set.getString("DNAME");
//               String loc = set.getString("LOC");
//
//               System.out.println(deptno + "\t" + dname + "\t" + loc);
//            }
//            set.close();
            while (set.next()){
                int deptno = set.getInt("id");
                String dname = set.getString("username");
                String loc = set.getString("userpass");
                int age = set.getInt("age");
                ListSql listSql = new ListSql(deptno,dname,loc,age);
                list.add(listSql);
            }
            list.forEach(System.out::println);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
class ListSql{
    private int depton;
    private String dname;
    private String loc;
    private int age;

    public ListSql(int depton, String dname, String loc,int age) {
        this.depton = depton;
        this.dname = dname;
        this.loc = loc;
        this.age =age;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ListSql listSql = (ListSql) o;
        return depton == listSql.depton && Objects.equals(dname, listSql.dname) && Objects.equals(loc, listSql.loc);
    }

    @Override
    public int hashCode() {
        return Objects.hash(depton, dname, loc);
    }

    @Override
    public String toString() {
        return "ListSql{" +
                "编号=" + depton +
                ", 用户名='" + dname + '\'' +
                ", 密码='" + loc + '\'' +
                ", 年龄=" + age +
                '}';
    }
}